package com.example.chatzsyproject.ui;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.example.chatzsyproject.R;
import com.example.chatzsyproject.app.MyApplication;
import com.example.chatzsyproject.fragment.FragmentConversation;
import com.example.chatzsyproject.fragment.FragmentFour;
import com.example.chatzsyproject.fragment.FragmentThree;
import com.example.chatzsyproject.fragment.FragmentTwo;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.contenter)
    FrameLayout contenter;
    @BindView(R.id.rg_group)
    RadioGroup rgGroup;
    /**
     * 上次点击返回键的时间
     */
    private long lastBackPressed;
    /**
     * 上次点击返回键的时间
     */
    private static final int QUIT_INTERVAL = 2500;

    private FragmentTransaction transaction;
    FragmentConversation fragmentOne = null;
    //    private FragmentOne fragmentOne = null;
    private FragmentTwo fragmentTwo = null;
    private FragmentThree fragmentThree = null;
    private FragmentFour fragmentFour = null;

    private RadioButton rb_three;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyApplication.addActivity(this);
        ButterKnife.bind(this);
        MyApplication.getScreen(this);
        rb_three = findViewById(R.id.rb_three);
        initView();
    }

    private void initView() {
        setTabSelection(1);
        rgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb_one:
                        setTabSelection(1);
                        break;
                    case R.id.rb_tow:
                        setTabSelection(2);
                        break;
                    case R.id.rb_three:
                        setTabSelection(3);
                        break;
                    case R.id.rb_four:
                        setTabSelection(4);
                        break;
                }
            }
        });
    }

    private void setTabSelection(int index) {
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        transaction = getSupportFragmentManager().beginTransaction();
        // 设置动画效果
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        hideFragments(transaction);

        switch (index) {
            case 1:
                if (fragmentOne == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
//                    fragmentOne = new FragmentOne();
                    fragmentOne = new FragmentConversation();
                    transaction.add(R.id.contenter, fragmentOne);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(fragmentOne);
                }
                break;
            case 2:
                if (fragmentTwo == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    fragmentTwo = new FragmentTwo();
                    transaction.add(R.id.contenter, fragmentTwo);

                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(fragmentTwo);
                }
                break;
            case 3:
                if (fragmentThree == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    fragmentThree = new FragmentThree();
                    transaction.add(R.id.contenter, fragmentThree);

                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(fragmentThree);
                }
                break;
            case 4:
                if (fragmentFour == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    fragmentFour = new FragmentFour();
                    transaction.add(R.id.contenter, fragmentFour);

                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(fragmentFour);
                }
                break;

        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {

        if (fragmentOne != null) {
            transaction.hide(fragmentOne);
        }
        if (fragmentTwo != null) {
            transaction.hide(fragmentTwo);
        }
        if (fragmentThree != null) {
            transaction.hide(fragmentThree);
        }
        if (fragmentFour != null) {
            transaction.hide(fragmentFour);
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (keyCode == KeyEvent.KEYCODE_BACK) { // 表示按返回键 时的操作
                long backPressed = System.currentTimeMillis();
                if (backPressed - lastBackPressed > QUIT_INTERVAL) {
                    lastBackPressed = backPressed;
                    Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_LONG).show();
                } else {
                    moveTaskToBack(false);
                    MyApplication.closeApp(this);
                    finish();
                }
            }
        }
        return false;
    }
}
